package com.fanghaoran.view.user.studyPlanFrame;

import com.fanghaoran.controller.StudyInfoController;
import com.fanghaoran.view.user.BackGroundPanel;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * @Author: win11
 * @Date: 2023/03/29/20:08
 * @Description:
 */
public class ModifyPlanFrame extends JFrame {

    public ModifyPlanFrame() throws IOException {
        //最外层界面frame的设置
        initFrame();
        //界面中的视图,控件等的设置
        initView();
        //设置可见
        this.setVisible(true);
    }

    private void initView() throws IOException {
        //panel来放组件
        BackGroundPanel panel = new BackGroundPanel(ImageIO.read(new File("src//image//rBackground.jpg")));
        //用垂直box实现组件垂直排列
        Box vBox = Box.createVerticalBox();
        vBox.add(Box.createVerticalStrut(100));

        Box box1=Box.createHorizontalBox();
        JLabel text=new JLabel("今日计划学习单词数 ");
        List<String> selectList = new ArrayList<>();
        for (int i = 0; i < 500; i++) {
            selectList.add(i+"");
        }
        //转成vector对象
        Vector<String> vector = new Vector<>(selectList);
        //选项列表
        JComboBox<String> selector = new JComboBox<>(vector);
        box1.add(text);
        box1.add(Box.createHorizontalStrut(20));
        box1.add(new JScrollPane(selector));

        Box box2=Box.createHorizontalBox();
        JButton confirm=new JButton("确定");
        box2.add(confirm);

        vBox.add(box1);
        vBox.add(Box.createVerticalStrut(50));
        vBox.add(box2);

        panel.add(vBox);

        this.add(panel);

        //选项条目列表点击事件
        selector.addItemListener(e -> {
                    if (selector.getSelectedItem() != null) {
                        for (String s : selectList) {
                            if (selector.getSelectedItem().equals(s)) {
                                //设置计划学习单词数
                                StudyPlanPanel.plan.setText(s);
                            }
                        }
                    }
                }
        );
        //确定按钮点击事件
        confirm.addActionListener(e-> {
            //更新数据库
            try {
                new StudyInfoController().updateStudyPlan(StudyPlanPanel.user.getId());
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            JOptionPane.showMessageDialog(panel,"设定成功");
                this.dispose();
            }
        );
    }

    private void initFrame() throws IOException {
        //设置宽高
        this.setSize(400, 400);
        //设置居中
        this.setLocationRelativeTo(null);
        //设置标题
        this.setTitle("扇贝单词");
        //设置标题旁边的logo图标
        this.setIconImage(ImageIO.read(new File("src//image//logo.jpg")));
        //设置置顶
        this.setAlwaysOnTop(true);
        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }
}
